<?php

use console\db\Migration;

/**
 * 创建动态/动态相关信息表
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m190102_180239_t_dynamic extends Migration {

    /**
     * {@inheritdoc}
     */
    public function up() {

        /**
         * 动态动态表
         * @table `%dynamic`
         */
        $this->tableBuilder([
            'name' => 'sns_dynamic',
            'comment' => '动态动态表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'group_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('所属圈子ID'),
                'topic_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('所属话题ID'),
                'subject' => $this->string(255)->defaultValue('')->comment('动态'),
                'format' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(0)->comment('内容格式：0文字，1视频，2单图，3多图'),
                'weight' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(0)->comment('权重'),
                'best_at' => $this->unixTimestamp()->comment('设为精华时间'),
                'recommend_at' => $this->unixTimestamp()->comment('推荐时间'),
                'location' => $this->string(50)->defaultValue('')->comment('所在地'),
                'gis' => $this->geometry()->comment('空间位置信息'),
                'geohash' => $this->geometryHash('gis'),
                'close_comment' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(0)->comment('关闭评论'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ],
            'index' => [
                [
                    'columns' => 'user_id',
                ],
                [
                    'columns' => 'topic_id',
                ],
                [
                    'columns' => 'format',
                ],
                [
                    'columns' => 'weight',
                ],
            ]
        ]);

        /**
         * 动态内容表
         * @table `%thread`
         */
        $this->tableBuilder([
            'name' => 'sns_dynamic_content',
            'comment' => '主题动态内容表',
            'columns' => [
                'id' => $this->primaryKey(),
                'dynamic_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('主题ID'),
                'body' => $this->text()->comment('内容'),
                'attachs' => $this->text()->comment('附件内容'),
                'format' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('格式'),
            ],
            'index' => [
                [
                    'columns' => 'dynamic_id',
                ]
            ]
        ]);

        /**
         * 动态图库信息表
         * @table `dynamic_gallery`
         */
        $this->tableBuilder([
            'name' => 'sns_dynamic_gallery',
            'comment' => '动态图库信息表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'dynamic_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('动态ID'),
                'comment_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('评论ID'),
                'original' => $this->char(255)->notNull()->comment('原图地址'),
                'description' => $this->string(255)->defaultValue('')->comment('描述'),
                'listorder' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(0)->comment('排序'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ],
            'index' => [
                [
                    'columns' => 'dynamic_id',
                ],
                [
                    'columns' => 'user_id',
                ]
            ]
        ]);

        /**
         * 动态视频表
         * @table `dynamic_video`
         */
        $this->tableBuilder([
            'name' => 'sns_dynamic_video',
            'comment' => '动态视频表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'dynamic_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('动态ID'),
                'path' => $this->string(255)->notNull()->comment('视频路径'),
                'duration' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('时长'),
                'resolution' => $this->float()->defaultValue(1)->comment('视频比例'),
                'video_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('视频ID'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ],
            'index' => [
                [
                    'columns' => 'dynamic_id',
                ],
                [
                    'columns' => 'user_id',
                ]
            ]
        ]);

        /**
         * 动态评论表
         * @todo create table `%dynamic_comment`.
         */
        $this->tableBuilder([
            'subsize' => 8,
            'name' => 'sns_dynamic_comment',
            'comment' => '动态评论表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'dynamic_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('动态ID'),
                'comment_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('评论ID'),
                'reply_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('回复ID'),
                'post' => $this->text()->notNull()->comment('评论内容'),
                'attachs' => $this->text()->comment('评论附件'),
                'author' => $this->string(50)->defaultValue('')->comment('作者'),
                'listener' => $this->string(50)->defaultValue('')->comment('被回复人'),
                'likes' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('点赞数'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ],
            'index' => [
                [
                    'columns' => 'dynamic_id',
                ],
                [
                    'columns' => 'comment_id',
                ],
                [
                    'columns' => 'user_id',
                ]
            ]
        ]);

        /**
         * 动态评论点赞表
         * @todo create table `%dynamic_like`.
         */
        $this->tableBuilder([
            'name' => 'sns_dynamic_like',
            'comment' => '动态评论点赞表',
            'columns' => [
                'id' => $this->primaryKey(),
                'dynamic_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('动态ID'),
                'comment_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('评论ID'),
                'author_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('主题或者评论的作者用户ID'),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => 'dynamic_id',
                ],
                [
                    'columns' => 'comment_id',
                ],
                [
                    'columns' => 'user_id',
                ],
            ]
        ]);

        /**
         * 动态统计表
         * @todo create table `%dynamic_stats`.
         */
        $this->tableBuilder([
            'name' => 'sns_dynamic_stats',
            'comment' => '动态统计表',
            'columns' => [
                'id' => $this->primaryKey(),
                'dynamic_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('动态ID'),
                'views' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('总浏览数'),
                'likes' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('点赞数'),
                'comments' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('评论数'),
                'shares' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('分享数'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => 'dynamic_id',
                ]
            ]
        ]);

        /**
         * 动态通知表
         * @todo create table `%sns_dynamic_notice`.
         */
        $this->tableBuilder([
            'name' => 'sns_dynamic_notice',
            'comment' => '动态通知表',
            'columns' => [
                'id' => $this->primaryKey(),
                'author_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('作者ID'),
                'dynamic_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('动态ID'),
                'comment_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('评论ID'),
                'reply_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('回复ID'),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'type' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(0)->comment('类型'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => 'dynamic_id',
                ]
            ]
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function down() {
        $this->dropTable('{{%sns_dynamic_notice}}');
        $this->dropTable('{{%sns_dynamic_stats}}');
        $this->dropTable('{{%sns_dynamic_like}}');
        $this->dropTable('{{%sns_dynamic_comment}}', 8);
        $this->dropTable('{{%sns_dynamic_video}}');
        $this->dropTable('{{%sns_dynamic_gallery}}');
        $this->dropTable('{{%sns_dynamic_content}}');
        $this->dropTable('{{%sns_dynamic}}');
    }

}
